using UnityEngine;
using System.Collections;

public class Minion : MonoBehaviour {

	public enum MinionPath {Top, Mid, Bot};
	public enum MinionState {MovingToFirstTarget, Attacking, MovingToXin}
	//public enum MinionType {Range, Melee};
	public MinionPath myPath;
	public MinionState myState;
	public GameObject top, mid, bot, xin;

	Vector3 startPoint, checkPoint;
	float lerpFactor = 0f;
	float travelTime, distance;
	float timer = 0f;

	GameObject target, wayPt, targetWP1, targetWP2;
	int currentTarget = 1;
	int ID;
	static int IDCounter = 0;
	public int hp;
	//public MinionType myType;
	public int damage;
	bool atkCD = false;
	// Use this for initialization
	void Start () {
		hp = 140;
		damage = 7;
		top = GameObject.Find ("TopWP");
		mid = GameObject.Find ("MidWP");
		bot = GameObject.Find ("BotWP");
		xin = GameObject.Find ("XinWP");
		startPoint = transform.position;

		travelTime = 3; 

		MarchOn ();
	}

	void RecalculatePath(Vector3 v1, Vector3 v2){
		distance = Distance (v1, v2);
	}
	// Update is called once per frame
	void Update () {
		//MarchOn();
		if(hp <= 0){
			Destroy (gameObject);

		}

		if (atkCD && (Time.time - timer) >= 1.0f)
		{
			atkCD = false;
			timer = Time.time;
		}

		if(myState == MinionState.MovingToFirstTarget){
			if(lerpFactor < 1.0f)
				lerpFactor += (1.0f/travelTime) * Time.deltaTime;
			else
				lerpFactor = 1.0f;
			
			transform.position = Vector3.Lerp(startPoint, targetWP1.transform.position, lerpFactor);

			if(transform.position == targetWP1.transform.position){
				if(target.name != "XinZhao"){
					myState = MinionState.Attacking; 

				}
				else{
					myState = MinionState.MovingToXin;
					lerpFactor = 0;
				}
			}
		}
		else if(myState == MinionState.Attacking){
			if(target!=null){
				if(!atkCD){
					if(target.GetComponent<Champion>().hp - damage <= 0){
						myState = MinionState.MovingToXin;
						RecalculatePath(transform.position, xin.transform.position);
						lerpFactor = 0f;
					}
					target.GetComponent<Champion>().hp -= damage;
					timer = Time.time;
					atkCD = true;
				}
			}
			else {

				myState = MinionState.MovingToXin;
				RecalculatePath(transform.position, xin.transform.position);
				lerpFactor = 0f;
				target = GameObject.Find ("XinZhao");
			}
		}
		else{
			if(lerpFactor < 1.0f)
				lerpFactor += (1.0f/travelTime) * Time.deltaTime;
			else
				lerpFactor = 1.0f;

			transform.position = Vector3.Lerp(checkPoint, targetWP2.transform.position, lerpFactor);

			if(lerpFactor == 1f){
				myState = MinionState.Attacking;
			}
		}
	}

	void Attack(GameObject target){

	}

	public void MoveToTarget(){
		if(myPath == MinionPath.Top){

		}
		else if(myPath == MinionPath.Mid){

		}
		else{

		}
	}

	public void MarchOn(){
		if(myPath == MinionPath.Top){
			if(GameObject.Find ("Garen") != null){
				target = GameObject.Find ("Garen");
			}
			else{
				target = GameObject.Find ("XinZhao");
			}
			targetWP1 = top;
			checkPoint = top.transform.position;
			distance = Distance(transform.position, top.transform.position);
		}
		else if(myPath == MinionPath.Mid){
			if(GameObject.Find ("Lux") != null){
				target = GameObject.Find ("Lux");
			}
			else{
				target = GameObject.Find ("XinZhao");
			}
			targetWP1 = mid;
			checkPoint = mid.transform.position;
			distance = Distance(transform.position, mid.transform.position);
		}
		else{
			if(GameObject.Find ("Vayne") != null){
				target = GameObject.Find ("Vayne");
			}
			else{
				target = GameObject.Find ("XinZhao");
			}
			targetWP1 = bot;
			checkPoint = bot.transform.position;
			distance = Distance(transform.position, bot.transform.position);
		}
		targetWP2 = xin;
	}

	float Distance(Vector3 p1, Vector3 p2){
		float dist = Mathf.Pow( (p2.x - p1.x), 2) + Mathf.Pow ( (p2.y - p1.y), 2);
		return Mathf.Sqrt(dist);
	}

	public void SetID()
	{
		ID = IDCounter;
		IDCounter++;
	}

	public int GetID()
	{
		return ID;
	}
}
